import VueRouter from 'vue-router';

import About from '../components/About.vue'
import Home from '../components/Home.vue'
import News from '../components/News.vue'
import Message from '../components/Message.vue'
import Detail from '../components/Detail.vue'

const router = new VueRouter({
    // mode:'history',
    mode:'hash',
    routes:[
        {
            name:'guanyu',
            path:'/About',
            component:About,
            meta:{isAuth:true,title:'关于'}
        },
        {
            path:'/Home',
            component:Home,
            children:[
                {
                    path:'Message',
                    component:Message,
                    children:[
                        {
                            name:'mingxi',
                            path:'Detail/:id/:name',
                            component:Detail,
                        }
                    ],
                    beforeEnter:(to,from,next) =>{
                        if(to.path === '/Home/Message'){
                            if(localStorage.getItem('username') === 'xfj'){
                                next()
                            }else{
                                alert("当前角色无法进行登录")
                            }
                        }else{
                            next()
                        }
                    }
                },
                {
                    path:'News',
                    component:News
                }
            ],
            meta:{isAuth:true,title:'主页'}
        },
    ]
})


//前置路由守卫 next 放行  to 去的路劲 from 来自哪个地址
router.beforeEach((to,from,next)=>{
    console.log(to,from,next)
    if(to.path === '/Home/Message'){
        if(localStorage.getItem('username') === 'xfj'){
            next()
        }else{
            alert("当前角色无法进行登录")
        }
    }else{
        next()
    }
})


//后置路由守卫
router.afterEach((to,from)=>{
    console.log('afterEach',to,from);
    document.title == to.meta.title
})



export default router;